﻿using Janus.BaseEntitys;
using Janus.Cores.Cacheing;
using Janus.Mes.Services.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Mvc;

namespace Janus.Mes.WebClient.Areas.System.Controllers
{
    public class ModuleController : BaseController
    {
        List<ModuleDTO> GetModuleList()
        {
            List<ModuleDTO> moduleList = HelperCommon.GetModulesFromXML();
            moduleList= moduleList.Where(c => string.IsNullOrEmpty(c.Parent)).ToList();
            // serviceModule.GetAll(base.SearchParma);
            //HttpContext.Cache["moduleList"] = moduleList;
            return moduleList;
        }

        public ModuleController()
        {            
        }
        public ActionResult Index()
        {
            return View(GetModuleList());
        }

        [HttpPost]
        public string Index(FormCollection collection)
        {
            try
            {
                var moduleList = HelperCommon.GetModulesFromXML(); //HttpContext.Cache["moduleList"] as List<ModuleDTO>;
                var retult = false;
                if (moduleList != null && moduleList.Count > 0)
                {
                    retult = true;// moduleService.Replace(moduleList);
                }

                if (retult)
                {
                    ajaxStr = base.AjaxResultSucessNoForward;
                }
                else
                {
                    ajaxStr = base.AjaxResultFail;
                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                ajaxStr = base.AjaxResultFail;
            }
            return "{" + ajaxStr + "}";
        }

        public string ReSetRoles()
        {
            try
            {
                CacheHelper.ClearCache("allValidModules");
                base.CurentUser.Modules = HelperCommon.GetModulesFromXML();
                ajaxStr = base.AjaxResultSucessNoForward;
            }
            catch (Exception ex)
            {
                ajaxStr = base.AjaxActionFail(ex.Message + ex.StackTrace);
            }
            return "{" + ajaxStr + "}";
        }
    }
}